package com.yk.i_wms.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yk.i_wms.common.model.PageResult;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.model.SmsOutStockDocDetail;
import com.yk.i_wms.service.ISmsOutStockDocDetailService;
import com.yk.i_wms.service.ISmsStockInfoService;
import com.yk.i_wms.util.EasyPoiExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出库单详情
 *
 * @author hbq
 * @date 2021-04-07 14:20:31
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "出库单详情")
@RequestMapping("sms")
public class SmsOutStockDocDetailController {
    @Autowired
    private ISmsOutStockDocDetailService smsOutStockDocDetailService;
    @Autowired
    private ISmsStockInfoService smsStockInfoService;
    /**
     * todo:根据备料号汇总数量信息
     */
    @ApiOperation(value = "根据备料号汇总数量信息")
    @PostMapping("/smsOutStockDocRecord/getQtyByDocNo")
    public Result getQtyByDocNo(@RequestBody Map<String, Object> params) {
        String docNo = MapUtil.getStr(params, "docNo");
        List<SmsOutStockDocDetail> smsOutStockDocDetails = smsOutStockDocDetailService.list(new LambdaQueryWrapper<SmsOutStockDocDetail>().eq(SmsOutStockDocDetail::getDocNo, docNo));
        List<Map> maps=new ArrayList<>();
        for (SmsOutStockDocDetail smsOutStockDocDetail : smsOutStockDocDetails) {
            Map<String, Object> map=new HashMap();
            map.put("playQty",smsOutStockDocDetail.getPlanQty());
            map.put("realQty",smsOutStockDocDetail.getRealQty());
            //实发数量
            //缺料数量=计划-发料-实发
        }
        return Result.succeed(maps,"查询成功");
    }

    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/smsOutStockDocDetail/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<Map> list= smsOutStockDocDetailService.findList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/smsOutStockDocDetail/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        SmsOutStockDocDetail model = smsOutStockDocDetailService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "新增or更新")
    @PostMapping("/smsOutStockDocDetail/save")
    public Result save(@RequestBody SmsOutStockDocDetail smsOutStockDocDetail) {
        smsOutStockDocDetailService.saveOrUpdate(smsOutStockDocDetail);
        return Result.succeed("保存成功");
    }

    /**
     * 批量新增or更新
     */
    @ApiOperation(value = "批量新增or更新")
    @PostMapping("/smsOutStockDocDetail/saveBatch")
    public Result saveBatch(@RequestBody Map<String,List<SmsOutStockDocDetail>> map) {
        List<SmsOutStockDocDetail> models = map.get("models");
        smsOutStockDocDetailService.saveOrUpdateBatch(models);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/smsOutStockDocDetail/del")
    public Result delete(@RequestBody Map<String,List<Long>> map) {
        List<Long> ids = map.get("ids");
        smsOutStockDocDetailService.removeByIds(ids);
        return Result.succeed("删除成功");
    }
    
    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/smsOutStockDocDetail/leadIn")
    public  Result leadIn(MultipartFile excel) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<SmsOutStockDocDetail> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, SmsOutStockDocDetail.class);
            rowNum = list.size();
            if (rowNum > 0) {
                list.forEach(u -> {
                        smsOutStockDocDetailService.save(u);
                });
                return Result.succeed("成功导入信息"+rowNum+"行数据");
            }
        }
        return Result.failed("导入失败");
    }
    
    /**
     * 导出（传入ids数组，选择指定id）
     */
    @ApiOperation(value = "导出（传入ids数组，选择指定id）")
    @PostMapping("/smsOutStockDocDetail/leadOut")
    public void leadOut(@RequestBody Map<String,List<Long>> map,HttpServletResponse response) throws IOException {
        List<Long> ids = map.get("ids");
        List<SmsOutStockDocDetail> smsOutStockDocDetailList = ids==null||ids.isEmpty()? smsOutStockDocDetailService.list():(List)smsOutStockDocDetailService.listByIds(ids);
        //导出操作
        EasyPoiExcelUtil.exportExcel(smsOutStockDocDetailList, "出库单详情导出", "出库单详情导出", SmsOutStockDocDetail.class, "smsOutStockDocDetail.xls", response);
    }
}
